//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO;
using System.Security;
using System.Security.Permissions;
using System.Windows;
using System.Windows.Interop;
using OpenLS.UI.Interop;

namespace OpenLS.UI
{
    public class OpenFileDialog : FileDialog
    {
        public override bool? ShowDialog(Window window)
        {
            if (!NativeMethods.IsVista)
                return showDialogXp(window);
            return ShowDialogVista(window);
        }


        private bool? ShowDialogVista(Window window)
        {
            IFileOpenDialog d = new IFileOpenDialog();
            if (Filter != null)
            {
                string[] filterArray = Filter.Split('|');
                //using (FilterCollection f = FilterCollection.Parse(Filter))
                {
                    try
                    {
                        d.SetFileTypes(filterArray.Length / 2, filterArray);//\\ ref f.array[0]);

                        //\\d.SetFileTypes(f.array.Length, ref f.array[0]);

                    }
                    catch (Exception)
                    {
                        d = new IFileOpenDialog();
            
                    }
                }
            }
            if (DefaultExt != null)
                d.SetDefaultExtension(DefaultExt);

            IntPtr handle = IntPtr.Zero;
            if (window != null)
                handle = new WindowInteropHelper( window).Handle;
            int res = d.Show(handle);
            if (res < 0)
                return false;
            GetFileNames(d);
            return true;
        }
        [SecurityCritical]
        public Stream OpenFile()
        {
            new FileDialogPermission(FileDialogPermissionAccess.Open).Demand();
            string text = FileNames[0];
            if (string.IsNullOrEmpty(text))
            {
                throw new InvalidOperationException("file names empty");
            }
            new FileIOPermission(FileIOPermissionAccess.Read, text).Assert();
            FileStream stream;
            try
            {
                stream = new FileStream(text, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            return stream;
        }

 

        bool? showDialogXp(Window window)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.AddExtension = AddExtension;
            dialog.DefaultExt = DefaultExt;
            dialog.FileName = FileName;
            dialog.Filter = Filter;
            dialog.InitialDirectory = InitialDirectory;
            dialog.RestoreDirectory = RestoreDirectory;
            bool? result = dialog.ShowDialog(window);
            FileName = dialog.FileName;
            FileNames = dialog.FileNames;
            
            return result;

        }

        public bool RestoreDirectory { get; set; }

        public string InitialDirectory { get; set; }

        void GetFileNames(IFileOpenDialog d)
        {
            IShellItemArray resultsArray;
            uint count;

            d.GetResults(out resultsArray);
            resultsArray.GetCount(out count);
            FileNames = new string[count];
            for (int i = 0; i < count; i++)
                FileNames[i] = (GetFileNameFromShellItem(GetShellItemAt(resultsArray, i)));
            if (FileNames.Length > 0)
                FileName = FileNames[0];
            else
                FileNames = null;
        }
       

    }
}
